import fetch from "src/utils/fetch";
import API from "src/utils/api";
import notification from "src/utils/notification";

export default {
  state: {
    configQueryInfoList: {
      list: []
    },

    handlerOpts: [],

    dsConfigOptFlag: false,
    dsConfigOpt: []
  },
  mutations: {
    // configQueryInfo列表数据
    SetConfigQueryInfo(state, data) {
      // 列表查询
      if (state.dsConfigOptFlag) {
        state.dsConfigOpt = [];
        state.dsConfigOptFlag = false;
        for (let key in data) {
          state.dsConfigOpt.push({ label: key.split("/")[3] });
        }
      } else {
        state.configQueryInfoList.list = [];

        for (let key in data) {
          state.configQueryInfoList.list.push({ key: key, content: data[key] });
        }
      }
    },
    SetConfigHandlers(state, data) {
      state.handlerOpts = [];
      if (data && data.length > 0) {
        data.forEach((item, index) => {
          let itemObj = JSON.parse(item);
          itemObj.value = itemObj.desc;
          state.handlerOpts.push(itemObj);
        });
      }
    }
  },
  actions: {
    // configQueryInfo 新增-编辑-删除
    doZooHandlerName: ({ state, commit }, params) => {
      let url = "";

      if (params.cb) {
        var cb = params.cb;
        delete params.cb;
      }

      if (params.type == "add") {
        url = API.batch.configAddConfig;
      } else if (params.type == "edit") {
        url = API.batch.configUpdateConfig;
      } else if (params.type == "del") {
        url = API.batch.configDeletedConfig;
      } else if (params.type == "batchDel") {
        url = API.batch.configBatchDeletedConfig;
      } else {
        return;
      }

      fetch({
        commit: commit,
        url: url,
        type: "post",
        data: params.data,
        showRegionLoading: true,
        success: data => {
          notification.success("操作成功!");
          cb && cb();
          if (params.queryParams) {
            // cb定义了说明是asset页面的操作
            params.queryParams.cb
              ? this.a.actions.queryAssetNode(
                  { state, commit },
                  params.queryParams
                )
              : this.a.actions.getConfigQueryInfo(
                  { state, commit },
                  params.queryParams
                );
          }
        }
      });
    },

    queryAssetNode: ({ state, commit }, params) => {
      fetch({
        url: "/config/queryNodeInfo",
        type: "post",
        data: { key: params.key },
        showRegionLoading: true,
        success: data => {
          params.cb(data);
        }
      });
    },

    getConfigQueryInfo: ({ state, commit }, params) => {
      var cb;
      let url = params.url
        ? API.batch.configShowValue
        : API.batch.configQueryInfo;

      if (params.dsConfigOptFlag) {
        state.dsConfigOptFlag = true;
        delete params.dsConfigOptFlag;
      } else if (params.cb) {
        cb = params.cb;
        delete params.cb;
        delete params.url;
      }

      fetch({
        commit: commit,
        url: url,
        type: "post",
        data: params.data ? params.data : params,
        showRegionLoading: true,
        success: data => {
          cb ? cb(data) : commit("SetConfigQueryInfo", data);
        }
      });
    },

    getConfigGetAllLeafValue: ({ state, commit }, params) => {
      fetch({
        commit: commit,
        url: API.batch.configGetAllLeafValue,
        type: "post",
        data: params,
        showRegionLoading: true,
        success: data => {
          commit("SetConfigQueryInfo", data);
        }
      });
    },

    getConfigGetHandlers: ({ state, commit }, params) => {
      fetch({
        commit: commit,
        url: API.batch.configGetHandlers,
        type: "post",
        data: params,
        showRegionLoading: true,
        success: data => {
          commit("SetConfigHandlers", data);
        }
      });
    }
  },
  getters: {
    configQueryInfoList: (state, rootState) => state.configQueryInfoList,
    handlerOpts: (state, rootState) => state.handlerOpts,
    dsConfigOpt: (state, rootState) => state.dsConfigOpt
  }
};
